package com.xiongjie.util;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.layout.font.FontProvider;

import java.io.FileOutputStream;
import java.io.IOException;

public class PdfUtil {
    private static String FONTPATH = "src/main/resources/font/NotoSansCJKsc-Regular.otf";

    public static void covert(String html, String pdfPath) {
        try {
            ConverterProperties properties = new ConverterProperties();
            FontProvider fontProvider = new DefaultFontProvider();
            FontProgram fontProgram = FontProgramFactory.createFont(FONTPATH);
            fontProvider.addFont(fontProgram);
            properties.setFontProvider(fontProvider);

            HtmlConverter.convertToPdf(html, new FileOutputStream(pdfPath), properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
